package cc.android.testapp.xp.hooker;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;

import org.json.JSONObject;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

import cc.android.testapp.cfg.CFG;
import cc.android.testapp.cfg.ConfigHelper;
import cc.android.testapp.util.CLog;
import cc.android.testapp.util.CTool;
import cc.android.testapp.xp.HookBase;
import cc.android.testapp.xp.HookModule;
import cc.commons.util.extra.CList;
import cc.commons.util.reflect.MethodUtil;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

@HookModule(packages = "com.tencent.qqlive", name = "腾讯视频")
public class HookQQLive extends HookBase implements ConfigHelper.IConfig {

    @CFG
    private String playPauseClass = "com.tencent.qqlive.mediaad.impl.i";

    @Override
    public void onConfigChange(JSONObject pNode) {
        this.mOldCfgStr = CTool.fieldVToStr(this);
        loadAnnoConfig(pNode);
    }

    @Override
    public String getCfgNode() {
        return "qqlive";
    }

    @Override
    protected void hookToApp(XC_LoadPackage.LoadPackageParam pParam, Application pApp) throws Throwable {
        ClassLoader tLoader = pApp.getClassLoader();
        Class<?> tClazz;
        try {
            // 启动页广告
            tClazz = tLoader.loadClass("com.tencent.qqlive.ona.activity.HomeActivity");
            hookMethodOnceIngnoreParam(tClazz, "initFragment", new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    Intent tIntent = ((Activity) param.thisObject).getIntent();
                    if (tIntent != null) {
                        tIntent.putExtra("home_need_splash", false);
                        tIntent.putExtra("showSplashAd", false);
                    }
                }
            });
        } catch (Throwable e) {
            CLog.log("启动页广告hook发生错误", e);
        }
        try {
            // 首页横幅广告
            tClazz = tLoader.loadClass("com.tencent.qqlive.universal.QQLiveCellParser");
            Arrays.stream(tClazz.getDeclaredMethods()).forEach((method) -> {
                if (method.getParameterTypes().length > 0
                        && method.getParameterTypes()[0].getSimpleName().equals("BlockType")) {
                    XposedBridge.hookMethod(method, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            if (((Enum) param.args[0]).name().contains("_AD_")) {
                                param.setResult(null);
                            }
                        }
                    });
                }
            });
        } catch (Throwable e) {
            CLog.log("首页横幅广告hook发生错误", e);
        }
        try {
            // APP后台切回广告
            tClazz = tLoader.loadClass("com.tencent.qqlive.ona.activity.HotStartSplashActivity");
            hookMethodOnceIngnoreParam(tClazz, "start", XC_MethodReplacement.DO_NOTHING);
        } catch (Throwable e) {
            CLog.log("APP后台切回广告hook发生错误", e);
        }
        try {
            // 用户中心顶部广告
            tClazz = tLoader.loadClass("com.tencent.qqlive.qadfeed.controller.QAdFeedBaseController");
            hookMethodAnyIngnoreParam(tClazz, "loadAd", XC_MethodReplacement.DO_NOTHING);
        } catch (Throwable e) {
            CLog.log("用户中心顶部广告hook发生错误", e);
        }
        try {
            // 用户中心中间卡片广告
            tClazz = tLoader.loadClass("com.tencent.qqlive.kmm.usercenter.ad.UserCenterAdCardKt");
            Arrays.stream(tClazz.getDeclaredMethods()).forEach((method) -> {
                if (Modifier.isStatic(method.getModifiers()) && method.getReturnType() == void.class) {
                    XposedBridge.hookMethod(method, XC_MethodReplacement.DO_NOTHING);
                }
            });
        } catch (Throwable e) {
            CLog.log("用户中心中间卡片广告hook发生错误", e);
        }
        try {
            // 视频播放页暂停广告
            tClazz = tLoader.loadClass(playPauseClass);
            hookMethodOnceIngnoreParam(tClazz, "onEvent", XC_MethodReplacement.DO_NOTHING);
        } catch (Throwable e) {
            CLog.log("视频播放页暂停广告hook发生错误(" + playPauseClass + ")", e);
        }
        try {
            // 视频播放页推荐卡片广告
            tClazz = tLoader.loadClass("com.tencent.qqlive.kmm.bridge.cell.VideoDetailBridgeCellParserGroup");
            Arrays.stream(tClazz.getSuperclass().getDeclaredMethods()).forEach(method -> {
                if (method.getParameterTypes().length > 0 && method.getParameterTypes()[0].getSimpleName().equals("BlockType")) {
                    XposedBridge.hookMethod(method, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            if (((Enum) param.args[0]).name().equals("BLOCK_TYPE_IWAN_RESOURCE_ITEM")) {
                                param.setResult(null);
                            }
                        }
                    });
                }
            });

        } catch (Throwable e) {
            CLog.log("视频播放页推荐卡片广告hook发生错误", e);
        }
    }
}
